<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Service extends Controller_ServiceHandler {

	public $secure_actions = array('showAllSupplierCategories'=>'');
	public $SUP_CATE_MISSING = 'SUP_CATE_MISSING';
	public $SUP_MISSING = 'SUP_MISSING';
	public $SEARCH_TXT_MISSING = 'SEARCH_TXT_MISSING';
	public $FEEDBACK_CONTENT_MISSING = 'FEEDBACK_CONTENT_MISSING';

	public function action_index()
	{

	}

	public function action_showAllSupplierCategories(){
		$result = ORM::factory("supplierCategory")->where("status","=","A")->find_all();
		echo SupplierCategoryParser::Instance()->parseSupplierCategoryResultToXml($result);
	}

	public function action_showAllSuppliers(){
		$result = ORM::factory("supplier")->where("status","=","A")->find_all();
		echo SupplierParser::Instance()->parseSupplierResultToXml($result);
	}

	public function action_showSuppliers(){
		$result = ORM::factory("supplier")->getResult($_REQUEST);
		echo SupplierParser::Instance()->parseSupplierResultToXml($result);
	}

	public function action_showSupplierDishes(){
		$msg = "丢失商家ID";
		if (!array_key_exists("supId", $_REQUEST)){
			Request::instance()->redirect('serviceError/showErrorMsg/'.$this->SUP_MISSING.'/'.$msg);
		}else{
			$supplierId = $_REQUEST["supId"];
			if (empty($supplierId)){
				Request::instance()->redirect('serviceError/showErrorMsg/'.$this->SUP_MISSING.'/'.$msg);
			}
			$result_dish_category = ORM::factory("dishCategory")->where("supplierid","=",$supplierId)->and_where("status","=","A")->find_all();
			echo SupplierDishParser::Instance()->parseDishResultToXml($result_dish_category);
		}
	}

	public function action_search(){
		$msg = "丢失搜索内容";
		if (!array_key_exists("qtxt", $_REQUEST)){
			Request::instance()->redirect('serviceError/showErrorMsg/'.$this->SEARCH_TXT_MISSING.'/'.$msg);
		}else{
			$supplierName = $_REQUEST["qtxt"];
			if (empty($supplierName)){
				Request::instance()->redirect('serviceError/showErrorMsg/'.$this->SEARCH_TXT_MISSING.'/'.$msg);
			}
			ORM::factory("transactionHistory")->searchAction($supplierName);
			$result = ORM::factory("supplier")->getResult($_REQUEST);
			echo SupplierParser::Instance()->parseSupplierResultToXml($result);
		}
	}

	public function action_searchSupplierById(){
		$msg = "丢失商家ID";
		if (!array_key_exists("supId", $_REQUEST)){
			Request::instance()->redirect('serviceError/showErrorMsg/'.$this->SEARCH_TXT_MISSING.'/'.$msg);
		}else{
			$supplierId = $_REQUEST["supId"];
			if (empty($supplierId)){
				Request::instance()->redirect('serviceError/showErrorMsg/'.$this->SEARCH_TXT_MISSING.'/'.$msg);
			}
			$result = ORM::factory("supplier",$supplierId);
			ORM::factory("transactionHistory")->scanAction($result->name);
			echo SupplierParser::Instance()->parseSupplierResultToXml($result);
		}
	}
	
	public function action_feedBack(){
		$msg = "丢失内容";
		if (!array_key_exists("desc", $_REQUEST)||!array_key_exists("type", $_REQUEST)){
			Request::instance()->redirect('serviceError/showErrorMsg/'.$this->FEEDBACK_CONTENT_MISSING.'/'.$msg);
		}else{
			$desc = $_REQUEST["desc"];
			$type = $_REQUEST["type"];
			$contact = "";
			if (empty($desc)||empty($type)){
				Request::instance()->redirect('serviceError/showErrorMsg/'.$this->FEEDBACK_CONTENT_MISSING.'/'.$msg);
			}
			if ($type == "E"){
				$desc = $desc.':::::SupplierId-'.$_REQUEST["supId"];
			}
			if (array_key_exists("contact", $_REQUEST)){
				$contact = $_REQUEST["contact"];
			}
			if (ORM::factory("feedBack")->insert($desc,$type,$contact)){
				echo ActionParser::Instance()->successMsg("");				
			}else{
				echo ActionParser::Instance()->errorMsg();	
			}
		}
	}
	
	public function action_getVersion(){
		echo VersionParser::Instance()->version();
	}
	
	public function action_consume(){
		$msg = "丢失商家ID";
		if (!array_key_exists("supId", $_REQUEST)){
			Request::instance()->redirect('serviceError/showErrorMsg/'.$this->SEARCH_TXT_MISSING.'/'.$msg);
		}else{
			$supplierId = $_REQUEST["supId"];
			if (empty($supplierId)){
				Request::instance()->redirect('serviceError/showErrorMsg/'.$this->SEARCH_TXT_MISSING.'/'.$msg);
			}
			$supplier = ORM::factory("supplier",$supplierId)->consume();
			if ($supplier){
				echo ActionParser::Instance()->successMsg($supplier->consumerecord);				
			}else{
				echo ActionParser::Instance()->errorMsg();	
			}
		}
	}
	
	public function action_areaList(){
		echo SupplierParser::Instance()->parseArea(ORM::factory("supplier")->areaList());
	}
	
	public function action_ads(){
		echo AdsParser::Instance()->parseAdResultToXml(ORM::factory("ads")->search($_REQUEST));
	}
	
	public function action_coupon(){
		echo CouponParser::Instance()->parseCouponResultToXml(ORM::factory("coupon")->search($_REQUEST));
	}
}
